﻿-- =============================================
-- Script Template
-- =============================================
use PDT
go

IF (OBJECT_ID('dbo.spStd_fillModuleParameter') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_fillModuleParameter'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_fillModuleParameter
GO
----------------------------------------------------------------------
--	dbo.spStd_fillModuleParameter
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_fillModuleParameter (
	@Module_Id int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	SELECT
		mp.ModuleParameter_Id, mp.[Name], mp.[Value], mp.StringValue, mp.Unit, mp.Nr
	FROM
		dbo.ModuleParameter mp
	WHERE 
		mp.Module_Id = @Module_Id 

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO



IF (OBJECT_ID('dbo.spStd_fillModuleChannel') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_fillModuleChannel'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_fillModuleChannel
GO
--	dbo.spStd_fillModuleChannel
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_fillModuleChannel (
	@Module_Id int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	SELECT
		c.*, mc.Nr
	FROM
		dbo.Channel c, dbo.Module_Channel mc 
	WHERE 
		c.Channel_Id = mc.Channel_Id AND
		mc.Module_Id = @Module_Id 

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO

IF (OBJECT_ID('dbo.spStd_deleteModuleChannel') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_deleteModuleChannel'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_deleteModuleChannel
GO
--	dbo.spStd_deleteModuleChannel
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_deleteModuleChannel (
	@Module_Id int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	DELETE FROM dbo.Module_Channel WHERE Module_Id = @Module_Id

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO



IF (OBJECT_ID('dbo.spStd_fillModule') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_fillModule'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_fillModule
GO
--	dbo.spStd_fillModule
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_fillModule
WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	SELECT * FROM dbo.Module

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_getModule') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_getModule'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_getModule
GO
--	dbo.spStd_getModule
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_getModule(
	@Module_Id int,
	@ModuleType_Id int OUTPUT,
	@Name nvarchar(12) OUTPUT,
	@Description nvarchar(80) OUTPUT,
	@Used bit OUTPUT,
	@Config int OUTPUT
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	SELECT 
		@ModuleType_Id = ModuleType_Id,
		@Name = [Name],
		@Description = [Description],
		@Used = Used,
		@Config = Config
	FROM 
		dbo.Module
	WHERE 
		Module_Id = @Module_Id 

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_insertModule') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_insertModule'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_insertModule
GO
--	dbo.spStd_insertModule
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (Record is not inserted)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_insertModule(
	@Module_Id int OUTPUT,
	@ModuleType_Id int,
	@Name nvarchar(12),
	@Description nvarchar(80),
	@Used bit,
	@Config int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	INSERT INTO dbo.Module 
		(ModuleType_Id, [Name], [Description], Used, Config)
	VALUES
		(@ModuleType_Id, @Name, @Description, @Used, @Config)
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT

	SET @Module_Id = SCOPE_IDENTITY()

	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_updateModule') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_updateModule'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_updateModule
GO
--	dbo.spStd_updateModule
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (Record is not updated)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_updateModule(
	@Module_Id int,
	@ModuleType_Id int,
	@Name nvarchar(12),
	@Description nvarchar(80),
	@Used bit,
	@Config int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	UPDATE dbo.Module SET 
		ModuleType_Id = @ModuleType_Id, 
		[Name] = @Name,
		[Description] = @Description,
		Used = @Used,
		Config = @Config
	WHERE
		Module_Id = @Module_Id
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT

	SET @Module_Id = SCOPE_IDENTITY()

	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO



IF (OBJECT_ID('dbo.spStd_deleteModule') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_deleteModule'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_deleteModule
GO
--	dbo.spStd_deleteModule
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (Record is not inserted)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_deleteModule(
	@Module_Id int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	DELETE FROM dbo.Module WHERE Module_Id = @Module_Id 
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO




IF (OBJECT_ID('dbo.spStd_getModuleParameter') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_getModuleParameter'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_getModuleParameter
GO
--	dbo.spStd_getModuleParameter
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_getModuleParameter(
	@ModuleParameter_Id int,
	@Name nvarchar(12) OUTPUT,
	@Value float OUTPUT,
	@StringValue nvarchar(255) OUTPUT,
	@Unit nvarchar(8) OUTPUT
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	SELECT 
		@Name = p.[Name],
		@Value = p.[Value],
		@StringValue = p.StringValue,
		@Unit = p.Unit
	FROM 
		dbo.ModuleParameter p
	WHERE 
		p.ModuleParameter_Id = @ModuleParameter_Id 

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_getModuleIdByParamId') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_getModuleIdByParamId'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_getModuleIdByParamId
GO
--	dbo.spStd_getModuleIdByParamId
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_getModuleIdByParamId(
	@ModuleParameter_Id int,
	@Module_Id int OUTPUT
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	SELECT @Module_Id = Module_Id FROM dbo.ModuleParameter WHERE ModuleParameter_Id = @ModuleParameter_Id

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_insertModuleParameter') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_insertModuleParameter'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_insertModuleParameter
GO
--	dbo.spStd_insertModuleParameter
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (Record is not inserted)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_insertModuleParameter(
	@ModuleParameter_Id int OUTPUT,
	@Name nvarchar(12),
	@Value float,
	@StringValue nvarchar(255),
	@Unit nvarchar(8),
	@Nr int,
	@Module_Id int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	BEGIN TRANSACTION

	INSERT INTO dbo.ModuleParameter ([Module_Id], [Name], [Value], [StringValue], [Unit], [Nr])
		VALUES(@Module_Id,@Name,@Value,@StringValue,@Unit,@Nr)
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	SET @ModuleParameter_Id = SCOPE_IDENTITY()

	IF (@err <> 0)
	BEGIN
		ROLLBACK
		RETURN @err
	END
	IF (@cnt > 0)
	BEGIN
		COMMIT
		RETURN 0
	END
	ELSE
	BEGIN
		ROLLBACK
		RETURN -1
	END
GO

IF (OBJECT_ID('dbo.spStd_updateModuleParameter') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_updateModuleParameter'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_updateModuleParameter
GO
--	dbo.spStd_updateModuleParameter
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_updateModuleParameter(
	@ModuleParameter_Id int,
	@Name nvarchar(12),
	@Value float,
	@StringValue nvarchar(255),
	@Unit nvarchar(8)
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	IF (@Name = '' OR @Name IS NULL)		
		UPDATE dbo.ModuleParameter SET [Value] = @Value WHERE ModuleParameter_Id = @ModuleParameter_Id
	ELSE
		UPDATE dbo.ModuleParameter SET 
			[Name] = @Name,
			[Value] = @Value,
			[StringValue] = @StringValue,
			[Unit] = @Unit
		WHERE ModuleParameter_Id = @ModuleParameter_Id
			
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT

	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_deleteModuleParameter') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_deleteModuleParameter'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_deleteModuleParameter
GO
--	dbo.spStd_deleteModuleParameter
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_deleteModuleParameter(
	@ModuleParameter_Id int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	DELETE FROM dbo.ModuleParameter WHERE ModuleParameter_Id = @ModuleParameter_Id
			
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT

	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO

IF (OBJECT_ID('dbo.spStd_insertModuleChannel') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_insertModuleChannel'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_insertModuleChannel
GO
--	dbo.spStd_insertModuleChannel
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (Record is not inserted)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_insertModuleChannel(
	@Module_Id int,
	@Channel_Id int,
	@Nr int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int


	INSERT INTO dbo.Module_Channel ([Module_Id], [Channel_Id], [Nr])
		VALUES(@Module_Id,@Channel_Id,@Nr)
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT

	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_fillModuleType') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_fillModuleType'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_fillModuleType
GO
--	dbo.spStd_fillModuleType
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_fillModuleType 
WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	SELECT ModuleType_Id, [Name], [Description] FROM dbo.ModuleType 

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_fillModuleTypeParameter') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_fillModuleTypeParameter'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_fillModuleTypeParameter
GO
----------------------------------------------------------------------
--	dbo.spStd_fillModuleTypeParameter
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_fillModuleTypeParameter (
	@ModuleType_Id int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	SELECT
		mp.ModuleTypeParameter_Id, mp.[Name], ISNULL(mp.[Value],0) as [Value], 
		ISNULL(mp.StringValue,'') as StringValue, ISNULL(mp.Unit,'') as Unit, mp.Nr
	FROM
		dbo.ModuleTypeParameter mp 
	WHERE 
		mp.ModuleType_Id = @ModuleType_Id 

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_fillModuleTypeChannelType') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_fillModuleTypeChannelType'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_fillModuleTypeChannelType
GO
----------------------------------------------------------------------
--	dbo.spStd_fillModuleTypeChannelType
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_fillModuleTypeChannelType (
	@ModuleType_Id int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	SELECT
		ModuleType_Id, ChannelType_id, Nr, [Name]
	FROM
		dbo.ModuleType_ChannelType
	WHERE 
		ModuleType_Id = @ModuleType_Id 

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_getChannel') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_getChannel'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_getChannel
GO
----------------------------------------------------------------------
--	dbo.spStd_getChannel
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_getChannel (
	@Channel_Id int,
	@ChannelAddress_Id int OUTPUT,
	@ChannelType_Id int OUTPUT,
	@Name nvarchar(12) OUTPUT,
	@Description nvarchar(80) OUTPUT,
	@Used bit OUTPUT,
	@Interval float OUTPUT,
	@OrdNr int OUTPUT,
	@Transtype int OUTPUT,
	@Factor float OUTPUT,
	@Offset float OUTPUT,
	@Operation int OUTPUT,
	@SaveStat bit OUTPUT,
	@Sign int OUTPUT,
	@Interval1 float OUTPUT	
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	SELECT 
		@ChannelAddress_Id = c.ChannelAddress_Id,
		@ChannelType_Id = c.ChannelType_Id,
		@Name = c.[Name],
		@Description = c.[Description],
		@Used = c.Used,
		@Interval = c.Interval,
		@OrdNr = c.OrdNr,
		@Transtype = c.Transtype,
		@Factor = c.Factor,
		@Offset = c.Offset,
		@Operation = c.Operation,
		@SaveStat = c.SaveStat,
		@Sign = c.[Sign],
		@Interval1 = c.Interval1
	FROM 
		dbo.Channel c LEFT JOIN 
		(SELECT distinct MAX(bc.Channel ) AS Channel ,MAX(bc.Base_Ch) AS base_ch  FROM dbo.Base_Ch bc  
			GROUP BY bc.Channel ) as b
		ON c.Channel_Id = b.Channel
	WHERE 
		c.Channel_Id = @Channel_Id
		

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_insertChannel') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_insertChannel'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_insertChannel
GO
--	dbo.spStd_insertChannel
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (Record is not inserted)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_insertChannel(
	@Channel_Id int OUTPUT,
	@ChannelAddress_Id int,
	@ChannelType_Id int,
	@Name nvarchar(12),
	@Description nvarchar(80),
	@Used bit,
	@Interval float,
	@OrdNr int,
	@Transtype int,
	@Factor float,
	@Offset float,
	@Operation int,
	@SaveStat bit,
	@Sign int,
	@Interval1 float		
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	INSERT INTO [dbo].[Channel] 
		(
            [ChannelAddress_Id]
           ,[ChannelType_Id]
           ,[Name]
           ,[Description]
           ,[Used]
           ,[Interval]
           ,[OrdNr]
           ,[Transtype]
           ,[Factor]
           ,[Offset]
           ,[Operation]
           ,[SaveStat]
           ,[Sign]
           ,[Interval1]
		)
     VALUES
		(
			 @ChannelAddress_Id
			,@ChannelType_Id
			,@Name
			,@Description
			,@Used
			,@Interval
			,@OrdNr
			,@Transtype
			,@Factor
			,@Offset
			,@Operation
			,@SaveStat
			,@Sign
			,@Interval1
		)	

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	SET @Channel_Id = SCOPE_IDENTITY()

	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_updateChannel') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_updateChannel'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_updateChannel
GO
--	dbo.spStd_updateChannel
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_updateChannel(
	@Channel_Id int,
	@ChannelAddress_Id int,
	@ChannelType_Id int,
	@Name nvarchar(12),
	@Description nvarchar(80),
	@Used bit,
	@Interval float,
	@OrdNr int,
	@Transtype int,
	@Factor float,
	@Offset float,
	@Operation int,
	@SaveStat bit,
	@Sign int,
	@Interval1 float		
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	UPDATE [dbo].[Channel]
	   SET 
	      [ChannelAddress_Id] = @ChannelAddress_Id
	      ,[ChannelType_Id] = @ChannelType_Id
	      ,[Name] = @Name
	      ,[Description] = @Description
	      ,[Used] = @Used
	      ,[Interval] = @Interval
	      ,[OrdNr] = @OrdNr
	      ,[Transtype] = @Transtype
	      ,[Factor] = @Factor
	      ,[Offset] = @Offset
	      ,[Operation] = @Operation
	      ,[SaveStat] = @SaveStat
	      ,[Sign] = @Sign
	      ,[Interval1] = @Interval1
	 WHERE 
		[Channel_Id] = @Channel_Id

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT

	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO



IF (OBJECT_ID('dbo.spStd_deleteChannel') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_deleteChannel'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_deleteChannel
GO
--	dbo.spStd_deleteChannel
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_deleteChannel(
	@Channel_Id int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	DELETE FROM dbo.Channel WHERE Channel_Id = @Channel_Id
			
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT

	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO

IF (OBJECT_ID('dbo.spStd_fillChannel') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_fillChannel'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_fillChannel
GO
----------------------------------------------------------------------
--	dbo.spStd_fillChannel
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_fillChannel(
	@ChannelType_Id int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	IF @ChannelType_Id = 0
		SELECT [Channel_Id] ,[ChannelAddress_Id], [ChannelType_Id], [Name], [Description], [Used], [Interval],
		       [OrdNr], [Transtype], [Factor], [Offset], [Operation],  [SaveStat], [Sign], [Interval1]
		FROM [dbo].[Channel]
	ELSE
		SELECT [Channel_Id] ,[ChannelAddress_Id], [ChannelType_Id], [Name], [Description], [Used], [Interval],
		       [OrdNr], [Transtype], [Factor], [Offset], [Operation],  [SaveStat], [Sign], [Interval1]
		FROM [dbo].[Channel] WHERE [ChannelType_Id] = @ChannelType_Id 
	
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_fillChannelParameters') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_fillChannelParameters'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_fillChannelParameters
GO
----------------------------------------------------------------------
--	dbo.spStd_fillChannelParameters
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_fillChannelParameters (
	@Channel_Id int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	SELECT
		p.Parameter_Id, p.[Name], p.[Value], p.StringValue, p.Unit
	FROM
		dbo.Parameters p, dbo.Channel_Parameter cp 
	WHERE 
		cp.Parameter_Id = p.Parameter_Id AND
		cp.Channel_Id = @Channel_Id 

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO

IF (OBJECT_ID('dbo.spStd_getChannelIdByParamId') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_getChannelIdByParamId'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_getChannelIdByParamId
GO
--	dbo.spStd_getChannelIdByParamId
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_getChannelIdByParamId(
	@Parameter_Id int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int

	--!!! It can return several Channel_Id
	SELECT Channel_Id FROM dbo.Channel_Parameter WHERE Parameter_Id = @Parameter_Id

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_getChannelAddress') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_getChannelAddress'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_getChannelAddress
GO
----------------------------------------------------------------------
--	dbo.spStd_getChannelAddress
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_getChannelAddress (
	@ChannelAddress_Id [int],
	@Port [int] OUTPUT,
	@Drive [int] OUTPUT,
	@Unit [int] OUTPUT,
	@Subunit [int] OUTPUT,
	@AddressByte [int] OUTPUT,
	@AddressBit [int] OUTPUT,
	@Inverted [bit] OUTPUT,
	@Hi [int] OUTPUT,
	@Low [int] OUTPUT,
	@WordLength [int] OUTPUT,
	@On_Error [int] OUTPUT,
	@AsyncType [int] OUTPUT
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	SELECT 
		@Port = Port,
		@Drive = Drive,
		@Unit = Unit,
		@Subunit = Subunit,
		@AddressByte = AddressByte, 
		@AddressBit	= AddressBit,
		@Inverted = Inverted, 
		@Hi = Hi,
		@Low = Low,
		@WordLength = WordLength, 
		@On_Error = On_Error, 
		@AsyncType = AsyncType
	FROM 
		dbo.ChannelAddress
	WHERE 
		ChannelAddress_Id = @ChannelAddress_Id
		

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO



IF (OBJECT_ID('dbo.spStd_ChannelAddressExists') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_ChannelAddressExists'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_ChannelAddressExists
GO
----------------------------------------------------------------------
--	dbo.spStd_ChannelAddressExists
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_ChannelAddressExists (
	@ChannelAddress_Id [int]
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	SELECT @cnt = count(*) FROM dbo.ChannelAddress WHERE  ChannelAddress_Id = @ChannelAddress_Id
		
	SELECT @err = @@ERROR
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO



IF (OBJECT_ID('dbo.spStd_updateChannelAddress') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_updateChannelAddress'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_updateChannelAddress
GO
----------------------------------------------------------------------
--	dbo.spStd_updateChannelAddress
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_updateChannelAddress (
	@ChannelAddress_Id [int],
	@Port [int],
	@Drive [int],
	@Unit [int],
	@Subunit [int],
	@AddressByte [int],
	@AddressBit [int],
	@Inverted [bit],
	@Hi [int],
	@Low [int],
	@WordLength [int],
	@AsyncType [int]
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	UPDATE dbo.ChannelAddress SET 
		Port = @Port,
		Drive = @Drive,
		Unit = @Unit,
		Subunit = @Subunit,
		AddressByte = @AddressByte, 
		AddressBit	= @AddressBit,
		Inverted = @Inverted, 
		Hi = @Hi,
		Low = @Low,
		WordLength = @WordLength, 
--		On_Error = On_Error, 
		AsyncType = @AsyncType
	WHERE 
		ChannelAddress_Id = @ChannelAddress_Id
		

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_insertChannelAddress') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_insertChannelAddress'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_insertChannelAddress
GO
----------------------------------------------------------------------
--	dbo.spStd_insertChannelAddress
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (Record is not inserted)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_insertChannelAddress (
	@ChannelAddress_Id [int] OUTPUT,
	@Port [int],
	@Drive [int],
	@Unit [int],
	@Subunit [int],
	@AddressByte [int],
	@AddressBit [int],
	@Inverted [bit],
	@Hi [int],
	@Low [int],
	@WordLength [int],
	@AsyncType [int]
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	INSERT INTO [dbo].[ChannelAddress]
           ([Port]
           ,[Drive]
           ,[Unit]
           ,[Subunit]
           ,[AddressByte]
           ,[AddressBit]
           ,[Inverted]
           ,[Hi]
           ,[Low]
           ,[WordLength]
           ,[AsyncType])
     VALUES
           (@Port
           ,@Drive
           ,@Unit
           ,@Subunit
           ,@AddressByte
           ,@AddressBit
           ,@Inverted
           ,@Hi
           ,@Low
           ,@WordLength
           ,@AsyncType)
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	SET @ChannelAddress_Id = SCOPE_IDENTITY()

	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_deleteChannelAddress') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_deleteChannelAddress'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_deleteChannelAddress
GO
----------------------------------------------------------------------
--	dbo.spStd_deleteChannelAddress
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_deleteChannelAddress (
	@ChannelAddress_Id [int]
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	DELETE FROM dbo.ChannelAddress WHERE ChannelAddress_Id = @ChannelAddress_Id

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_insertBaseCh') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_insertBaseCh'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_insertBaseCh
GO
----------------------------------------------------------------------
--	dbo.spStd_insertBaseCh
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (Record is not inserted)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_insertBaseCh (
	@Channel int,
	@Base_Ch int,
	@Neg bit
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	INSERT INTO [dbo].[Base_Ch]
           ([Channel]
           ,[Base_Ch]
           ,[Neg])
     	VALUES
           (@Channel
           ,@Base_Ch
           ,@Neg)
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT

	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_deleteBaseCh') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_deleteBaseCh'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_deleteBaseCh
GO
----------------------------------------------------------------------
--	dbo.spStd_deleteBaseCh
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_deleteBaseCh (
	@Channel int,
	@Base_Ch int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	DELETE FROM [dbo].[Base_Ch] WHERE [Channel] = @Channel AND [Base_Ch] = @Base_Ch
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT

	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO


IF (OBJECT_ID('dbo.spStd_fillBaseCh') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_fillBaseCh'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_fillBaseCh
GO
----------------------------------------------------------------------
--	dbo.spStd_fillBaseCh
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_fillBaseCh (
	@Channel int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	IF @Channel = 0
		SELECT Channel, Base_Ch, Neg FROM dbo.base_ch
	ELSE
		SELECT Channel, Base_Ch, Neg FROM dbo.base_ch WHERE Channel = @Channel 

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1
GO

IF (OBJECT_ID('dbo.spStd_deleteBaseChList') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_deleteBaseChList'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_deleteBaseChList
GO
----------------------------------------------------------------------
--	dbo.spStd_deleteBaseChList
--	return:
--	=0 (Completed)
--	>0 (Database error)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_deleteBaseChList (
	@Channel int
) WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	DELETE FROM [dbo].[Base_Ch] WHERE [Channel] = @Channel
	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT

	IF (@err <> 0)
		RETURN @err
	ELSE
		RETURN 0
GO




IF (OBJECT_ID('dbo.spStd_fillChannelType') IS NOT NULL) AND (OBJECTPROPERTY(OBJECT_ID('dbo.spStd_fillChannelType'), 'IsProcedure') = 1)
	DROP PROCEDURE dbo.spStd_fillChannelType
GO
----------------------------------------------------------------------
--	dbo.spStd_fillChannelType
--	return:
--	=0 (Completed)
--	>0 (Database error)
--	-1 (List is empty)
----------------------------------------------------------------------
CREATE PROCEDURE dbo.spStd_fillChannelType 
WITH ENCRYPTION AS
	DECLARE @err int, @cnt int
	
	SELECT ChannelType_Id, Basetype, ISNULL([Description],'') as Description, ISNULL(Unit,'') as Unit FROM dbo.ChannelType

	SELECT @err = @@ERROR, @cnt = @@ROWCOUNT
	IF (@err <> 0)
		RETURN @err
	IF (@cnt > 0)
		RETURN 0
	ELSE
		RETURN -1





